
from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class fdkaacConan(ConanFile):
    name = "fdk-aac"
    version = "1.1.1"
    url = "https://gitee.com/ykhumanspider/fdk-aac.git"
    description = "fdk-aac"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    
    def source(self):
        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            tools.rmdir("./pkgsrc")
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url,branch="master")


    def build(self):
        cmake = CMake(self)
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))

        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="./autogen.sh")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                              bashcmd="./configure --prefix=" + tools.unix_path(self.package_folder))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make -j 8")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make install")
                try:
                    os.remove(tools.get_env("CONAN_SH_PATH")) 
                except e:
                    print(e)
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.run("copy  Makefile.vc pkgsrc ")
                self.run("cd pkgsrc && nmake -f Makefile.vc all")
                self.run("cd pkgsrc && nmake -f Makefile.vc  prefix=\"" + self.package_folder + "\" install")

            if tools.get_env("BUILD_ENV") == "msvc32":
                self.run("copy  Makefile.vc pkgsrc ")
                self.run("cd pkgsrc && nmake -f Makefile.vc all")
                self.run("cd pkgsrc && nmake -f Makefile.vc  prefix=\"" + self.package_folder + "\" install")

    
    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)
        self.copy("Makefile.vc",dst="",keep_path=True)



    def package_info(self):
        self.cpp_info.name = 'fdk-aac'
        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "fdk-aac"
                self.cpp_info.names["cmake_find_package_multi"] = "fdk-aac"
                self.cpp_info.libs = ['libfdk-aac.a']  # The libs to link against
                self.cpp_info.buildirs = 'pkgsrc'
                self.cpp_info.libdirs = ["lib"]  # Directories where libraries can be found
            if tools.get_env("BUILD_ENV") == "msvc64":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "fdk-aac"
                self.cpp_info.names["cmake_find_package_multi"] = "fdk-aac"
                self.cpp_info.libs = ['fdk-aac.lib']  # The libs to link against
            if tools.get_env("BUILD_ENV") == "msvc32":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "fdk-aac"
                self.cpp_info.names["cmake_find_package_multi"] = "fdk-aac"
                self.cpp_info.libs = ['fdk-aac.lib']  # The libs to link against
                